<template>
  <!-- 新增员工弹框 -->
  <!-- 表单 -->
  <el-form ref="addForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="form.username" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="form.mobile" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="form.timeOfEntry" placeholder="请选择入职时间" value-format="yyyy-MM-dd" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="form.formOfEmployment" placeholder="请选择">
        <el-option label="正式" :value="1" />
        <el-option label="非正式" :value="2" />
        <el-option label="未知" :value="3" />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="form.workNumber" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="form.departmentName" placeholder="请选中部门" @focus="departmentNameFocus" />
      <div v-if="showTree" class="tree-box">
        <el-tree :data="treeData" default-expand-all :props="{ label: 'name' }" @node-click="treeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="form.correctionTime" placeholder="请选择转正时间" value-format="yyyy-MM-dd" />
    </el-form-item>
    <el-form-item>
      <el-button @click="addCancel">取消</el-button>
      <el-button type="primary" @click="addSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addEmployee } from '@/api/employees'
import { getDepartmentList } from '@/api/department'
import { changeTreeFn } from '@/utils/changeTree'
export default {
  name: 'EmpDialog',
  data() {
    return {
      form: {
        username: '', // 用户名
        mobile: '', // 电话号码
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        formOfEmployment: ''// 聘用形式
      },
      showTree: false,
      treeData: [],
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'change' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'change' }
        ]
      }
    }
  },
  // created() {
  //   this.getDepartmentListFn()
  // },
  methods: {
    resetForm() {
      this.form = {
        username: '', // 用户名
        mobile: '', // 电话号码
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        formOfEmployment: ''// 聘用形式
      }
      this.$refs.addForm.resetFields()
    },
    // 点击取消按钮
    addCancel() {
      this.resetForm()
      this.$emit('close')
    },

    // 点击确定按钮
    addSubmit() {
      console.log(this.form)
      this.$refs.addForm.validate(async(valid) => {
        if (!valid) return
        const res = await addEmployee(this.form)
        console.log(res)
        this.$message.success('添加员工成功')
        this.$emit('changeList')
      })
    },
    async departmentNameFocus() {
      await this.getDepartmentListFn()
      this.showTree = true
    },
    treeClick(data) {
      // 如果当前部门还有子部门，则不能被选中
      if (data && data.children) {
        return
      }
      // 把当前选中的节点显示在 input 框中
      this.form.departmentName = data.name
      // 隐藏 tree
      this.showTree = false
    },
    // 获取所有部门
    async getDepartmentListFn() {
      const res = await getDepartmentList()
      this.treeData = changeTreeFn(res.data.depts, '')
    }
  }
}
</script>

<style>

</style>
